package com.ybwh.springboot2.util;

import com.google.common.base.Preconditions;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 处理excel文件的工具类,废弃，请使用阿里巴巴的easy-excel
 * </p>
 *
 * @author fanbeibei
 * @version V1.0
 * @since 2019/9/19 17:40
 */
@Deprecated
public class ExcelUtil {
    private ExcelUtil() {
    }

    public static final String V_2003 = "2003";
    public static final String V_2007 = "2007";


    /**
     * 从sheet中读取数据
     *
     * @param sheet        读数据的sheet
     * @param rowProcessor 行处理逻辑
     * @param <T>          读入数据类型
     * @return
     */
    public static <T> List<T> readSheet(Sheet sheet, int startRowIndex, int endRowIndex, RowProcessor<T> rowProcessor) {
        Preconditions.checkArgument(null != sheet);
        Preconditions.checkArgument(null != rowProcessor);


        int maxRowNum = sheet.getLastRowNum();
        if (endRowIndex > maxRowNum) {
            throw new IllegalStateException("maxRowNum is " + maxRowNum);
        }


        List<T> list = new ArrayList<>(endRowIndex - startRowIndex + 1);
        for (int i = startRowIndex; i <= endRowIndex; i++) {
            Row row = sheet.getRow(i);
            T d = rowProcessor.readRow(row, i);
            list.add(d);
        }

        return list;
    }

    /**
     * 将数据写入sheet,需要该sheet已创建好第0行表头标题,且设置好第一行的格式
     *
     * @param data         写入excel的数据
     * @param rowProcessor 行处理逻辑
     * @param <T>          写入数据类型
     * @return
     */
    public static <T> void writeTemplateSheet(Sheet templateSheet, List<T> data, RowProcessor<T> rowProcessor) {
        Preconditions.checkArgument(null != templateSheet);
        Preconditions.checkArgument(!CollectionUtils.isEmpty(data));
        Preconditions.checkArgument(null != rowProcessor);
        Preconditions.checkArgument(templateSheet instanceof HSSFSheet || templateSheet instanceof XSSFSheet,
                "only support HSSFSheet or XSSFSheet");


        CellStyle cellStyle = null;
        for (int i = 1; i <= data.size(); i++) {
            T d = data.get(i - 1);

            Row row = null;
            if (i == 1) {
                //第一行是格式模板
                row = templateSheet.getRow(i);
                cellStyle = row.getCell(0).getCellStyle();
            } else {
                //从第二行开始复制第一行格式
                row = templateSheet.createRow(i);
                row.setRowStyle(cellStyle);
            }
            rowProcessor.writeRow(d, row, i);
        }
    }

    /**
     * 写表格体
     *
     * @param sheet
     * @param startRowIndex 从第几行开始写(0,1,2,...)
     * @param data          写入excel的数据
     * @param rowProcessor  行处理逻辑
     * @param <T>           写入数据类型
     * @return
     */
    public static <T> void writeGridBody(Sheet sheet, List<T> data, int startRowIndex, RowProcessor<T> rowProcessor) {
        Preconditions.checkArgument(null != sheet);
        Preconditions.checkArgument(!CollectionUtils.isEmpty(data));
        Preconditions.checkArgument(null != rowProcessor);
        Preconditions.checkArgument(sheet instanceof HSSFSheet || sheet instanceof XSSFSheet,
                "only support HSSFSheet or XSSFSheet");

        for (T d : data) {
            Row row = sheet.createRow(startRowIndex);
            rowProcessor.writeRow(d, row, startRowIndex);
            startRowIndex++;
        }
    }

    /**
     * 创建头部
     *
     * @param sheet
     * @param headers
     */
    public static void writeHeader(Sheet sheet, List<String> headers, CellStyle style) {

        Preconditions.checkArgument(null != sheet);
        Preconditions.checkArgument(!CollectionUtils.isEmpty(headers));
        Preconditions.checkArgument(sheet instanceof HSSFSheet || sheet instanceof XSSFSheet,
                "only support HSSFSheet or XSSFSheet");

        Row headRow = sheet.createRow(0);
        headRow.setRowStyle(style);
        for (int i = 0; i < headers.size(); i++) {
            Cell cell = headRow.createCell(i);
            cell.setCellValue(headers.get(i));
        }
    }


    /**
     * 复制一个单元格样式到目的单元格样式
     *
     * @param fromStyle
     * @param toStyle
     */
    public static void copyCellStyle(CellStyle fromStyle,
                                     CellStyle toStyle) {
        toStyle.setAlignment(fromStyle.getAlignment());
        //边框和边框颜色
        toStyle.setBorderBottom(fromStyle.getBorderBottom());
        toStyle.setBorderLeft(fromStyle.getBorderLeft());
        toStyle.setBorderRight(fromStyle.getBorderRight());
        toStyle.setBorderTop(fromStyle.getBorderTop());
        toStyle.setTopBorderColor(fromStyle.getTopBorderColor());
        toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor());
        toStyle.setRightBorderColor(fromStyle.getRightBorderColor());
        toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor());

        //背景和前景
        toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor());
        toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor());

        toStyle.setDataFormat(fromStyle.getDataFormat());
        toStyle.setFillPattern(fromStyle.getFillPattern());
//		toStyle.setFont(fromStyle.getFont(null));
        toStyle.setHidden(fromStyle.getHidden());
        toStyle.setIndention(fromStyle.getIndention());//首行缩进
        toStyle.setLocked(fromStyle.getLocked());
        toStyle.setRotation(fromStyle.getRotation());//旋转
        toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment());
        toStyle.setWrapText(fromStyle.getWrapText());

    }


    /**
     * 将数据写入Workbook
     *
     * @param wb
     * @param sheetName    sheet名称
     * @param data         写入excel的数据
     * @param rowProcessor 行处理逻辑
     * @param <T>          写入数据类型
     */
    public static <T> void writeWorkbook(Workbook wb, String sheetName, List<T> data, RowProcessor<T> rowProcessor) {
        Preconditions.checkArgument(null != wb);
        Preconditions.checkArgument(wb instanceof HSSFWorkbook || wb instanceof XSSFWorkbook,
                "only support HSSFWorkbook or XSSFWorkbook");
        Sheet sheet = wb.createSheet(null == sheetName ? "" : sheetName);

//        writeSheet(sheet, data, rowProcessor);
    }


    /**
     * 行处理
     */
    public interface RowProcessor<T> {
        /**
         * 读取行
         *
         * @param row      行
         * @param rowIndex 行号,从0开始
         */
        default T readRow(Row row, int rowIndex) {
            return null;
        }


        /**
         * 写入行
         *
         * @param data     要写入的数据
         * @param row      行
         * @param rowIndex 行号,从0开始
         * @return 是否写入成功
         */
        default boolean writeRow(T data, Row row, int rowIndex) {
            return false;
        }
    }


    public static void main(String[] args) {


    }

}
